<?php
/**
getProvinceList($params)
getDistrictList($params)
getDistrictListFromProvinceId($params)
getTambonList($params)
getTambonListFromDistrictId($params)
getVillageList($params)
getVillageListFromTambonId($params)
* 
*
getHouseList($params)
getHouseListByDistrictId($params)
getHouseListByProvinceId($params)
getHouseListByTambonId($params)
getHouseListByVillageId($params)
* 
*
getLastPersonId($params)
* 
getPersonListByHouseId($params)
getPersonListByVillageId($params)
getPersonListByTambonId($params)
getPersonListByDistrictId($params)
getPersonListByProvinceId($params)
* 
*
getPersonQuantityByDistrictId($params)
getPersonQuantityByHouseId($params)
getPersonQuantityByProvinceId($params)
getPersonQuantityByTambonId($params)
getPersonQuantityByVillageId($params)
* 
* 
getVillageDetailById($params)
getTambonDetailById($params)
getDistrictDetailById($params)
getProvinceDetailById($params)
**/
class VoteDataCenterReader {

  private $host='';
  private $_MOD = '';
  private $ERROR = '';
  
  function __construct($host=NULL,$module=NULL){
    if(is_null($host)){
      $host = "http://www.votethailand.com/votedatacenter/";
    }
    if(is_null($module)){
      $module = 'VoteDataCenter';
    }
    
    //Prepare domain
    preg_match('@^(?:http://)?([^/].*)@i',$host,$matches);
    preg_match('@^(?:www.)?([^/].*)@i',$matches[1],$host);
    $host = rtrim($host[1],"/");
    $host = "http://".$host.'/index.php';
    
    
    $this->host = $host;
    $this->_MOD = $module;
  }
  public function getProvinceList($params=NULL){
    return $this->_call('getProvinceList',$params);
  }
  
  public function getDistrictList($params=NULL){
    return $this->_call('getDistrictList',$params);
  }
    
  public function getDistrictListByProvinceId($params=NULL){
    return $this->_call('getDistrictListByProvinceId',$params);
  }
    
  public function getTambonList($params=NULL){
    return $this->_call('getTambonList',$params);
  }
  
  public function getTambonListByDistrictId($params=NULL){
    return $this->_call('getTambonListByDistrictId',$params);
  }
  
  public function getVillageList($params=NULL){
    return $this->_call('getVillageList',$params);
  }
  
  public function getVillageListByTambonId($params=NULL){
    return $this->_call('getVillageListByTambonId',$params);
  }
  
  public function getHouseList($params=NULL){
    return $this->_call('getHouseList',$params);
  }
  
  public function getHouseListByDistrictId($params=NULL){
    return $this->_call('getHouseListByDistrictId',$params);
  }
  
  public function getHouseListByProvinceId($params=NULL){
    return $this->_call('getHouseListByProvinceId',$params);
  }
  
  public function getHouseListByTambonId($params=NULL){
    return $this->_call('getHouseListByTambonId',$params);
  }
  
  public function getHouseListByVillageId($params=NULL){
    return $this->_call('getHouseListByVillageId',$params);
  }
  
  public function getLastPersonId($params=NULL){
    return $this->_call('getLastPersonId',$params);
  }
  
  public function getPersonListByHouseId($params=NULL){
    return $this->_call('getPersonListByHouseId',$params);
  }
  
  public function getPersonListByVillageId($params=NULL){
    return $this->_call('getPersonListByVillageId',$params);
  }
    
  public function getPersonListByTambonId($params=NULL){
    return $this->_call('getPersonListByTambonId',$params);
  }
  
  public function getPersonListByDistrictId($params=NULL){
    return $this->_call('getPersonListByDistrictId',$params);
  }
  
  public function getPersonListByProvinceId($params=NULL){
    return $this->_call('getPersonListByProvinceId',$params);
  }
  
  public function getPersonQuantityByDistrictId($params=NULL){
    return $this->_call('getPersonQuantityByDistrictId',$params);
  }
  
  public function getPersonQuantityByHouseId($params=NULL){
    return $this->_call('getPersonQuantityByHouseId',$params);
  }
  
  public function getPersonQuantityByProvinceId($params=NULL){
    return $this->_call('getPersonQuantityByProvinceId',$params);
  }
  
  public function getPersonQuantityByTambonId($params=NULL){
    return $this->_call('getPersonQuantityByTambonId',$params);
  }
  
  public function getPersonQuantityByVillageId($params=NULL){
    return $this->_call('getPersonQuantityByVillageId',$params);
  }
  
  public function getProvinceDetailById($params=NULL){
    return $this->_call('getProvinceDetailById',$params);
  }
  
  public function getDistrictDetailById($params=NULL){
    return $this->_call('getDistrictDetailById',$params);
  }
  
  public function getTambonDetailById($params=NULL){
    return $this->_call('getTambonDetailById',$params);
  }
  
  public function getVillageDetailById($params=NULL){
    return $this->_call('getVillageDetailById',$params);
  }

  
  private function _call($func='',$params=''){
    switch ($func) {
      case  'getProvinceList':
            $paramUri = '';
            foreach ($params AS $key=>$val){
              $paramUri .= "&".$key."=".$val;
            }
            $url = $this->host."?module=".$this->_MOD."&type=ws&func=".$func.$paramUri;

            $xml = @file_get_contents($url);
            $pxml = simplexml_load_string($xml);
            
            $pxml = $this->_toArray($pxml);

            if($pxml['data']['title']=='ไม่มีข้อมูล'){
              $this->ERROR = "NO RECORD";
              return FALSE;
            }
            if(is_null($pxml['datas'])){
              return $pxml;
            }else{
              return $pxml['datas'];
            }
            break;
      case 'getDistrictList':
            $paramUri = '';
            foreach ($params AS $key=>$val){
              $paramUri .= "&".$key."=".$val;
            }
            $url = $this->host."?module=".$this->_MOD."&type=ws&func=".$func.$paramUri;

            $xml = @file_get_contents($url);
            $pxml = simplexml_load_string($xml);
            
            $pxml = $this->_toArray($pxml);
            if(is_null($pxml['datas'])){
              return $pxml;
            }else{
              return $pxml['datas'];
            }
            break;
      case 'getDistrictListByProvinceId':
            $paramUri = '';
            foreach ($params AS $key=>$val){
              $paramUri .= "&".$key."=".$val;
            }
            $url = $this->host."?module=".$this->_MOD."&type=ws&func=".$func.$paramUri;

            $xml = @file_get_contents($url);
            $pxml = simplexml_load_string($xml);
            
            $pxml = $this->_toArray($pxml);
            if(is_null($pxml['datas'])){
              return $pxml;
            }else{
              return $pxml['datas'];
            }
            break;
      case 'getTambonList':
            $paramUri = '';
            foreach ($params AS $key=>$val){
              $paramUri .= "&".$key."=".$val;
            }
            $url = $this->host."?module=".$this->_MOD."&type=ws&func=".$func.$paramUri;
            
            $xml = @file_get_contents($url);
            $pxml = simplexml_load_string($xml);
            
            $pxml = $this->_toArray($pxml);
            if(is_null($pxml['datas'])){
              return $pxml;
            }else{
              return $pxml['datas'];
            }
            break;
      case 'getTambonListByDistrictId':
            $paramUri = '';
            foreach ($params AS $key=>$val){
              $paramUri .= "&".$key."=".$val;
            }
            $url = $this->host."?module=".$this->_MOD."&type=ws&func=".$func.$paramUri;
            
            $xml = @file_get_contents($url);
            $pxml = simplexml_load_string($xml);
            
            $pxml = $this->_toArray($pxml);
            if(is_null($pxml['datas'])){
              return $pxml;
            }else{
              return $pxml['datas'];
            }
            break;
      case 'getVillageList':
            $paramUri = '';
            foreach ($params AS $key=>$val){
              $paramUri .= "&".$key."=".$val;
            }
            $url = $this->host."?module=".$this->_MOD."&type=ws&func=".$func.$paramUri;
            
            $xml = @file_get_contents($url);
            $pxml = simplexml_load_string($xml);
            
            $pxml = $this->_toArray($pxml);
            if(is_null($pxml['datas'])){
              return $pxml;
            }else{
              return $pxml['datas'];
            }
            break;
      case 'getVillageListByTambonId':
            $paramUri = '';
            foreach ($params AS $key=>$val){
              $paramUri .= "&".$key."=".$val;
            }
            $url = $this->host."?module=".$this->_MOD."&type=ws&func=".$func.$paramUri;
            
            $xml = @file_get_contents($url);
            $pxml = simplexml_load_string($xml);
            
            $pxml = $this->_toArray($pxml);
            if(is_null($pxml['datas'])){
              return $pxml;
            }else{
              return $pxml['datas'];
            }
            break;
      case 'getHouseList':
            $paramUri = '';
            foreach ($params AS $key=>$val){
              $paramUri .= "&".$key."=".$val;
            }
            $url = $this->host."?module=".$this->_MOD."&type=ws&func=".$func.$paramUri;
            
            $xml = @file_get_contents($url);
            $pxml = simplexml_load_string($xml);
            
            $pxml = $this->_toArray($pxml);
            if(is_null($pxml['datas'])){
              return $pxml;
            }else{
              return $pxml['datas'];
            }
            break;
      case 'getHouseListByDistrictId':
            $paramUri = '';
            foreach ($params AS $key=>$val){
              $paramUri .= "&".$key."=".$val;
            }
            $url = $this->host."?module=".$this->_MOD."&type=ws&func=".$func.$paramUri;
            
            $xml = @file_get_contents($url);
            $pxml = simplexml_load_string($xml);
            
            $pxml = $this->_toArray($pxml);
            if(is_null($pxml['datas'])){
              return $pxml;
            }else{
              return $pxml['datas'];
            }
            break;
      case 'getHouseListByProvinceId':
            $paramUri = '';
            foreach ($params AS $key=>$val){
              $paramUri .= "&".$key."=".$val;
            }
            $url = $this->host."?module=".$this->_MOD."&type=ws&func=".$func.$paramUri;
            
            $xml = @file_get_contents($url);
            $pxml = simplexml_load_string($xml);
            
            $pxml = $this->_toArray($pxml);
            if(is_null($pxml['datas'])){
              return $pxml;
            }else{
              return $pxml['datas'];
            }
            break;
      case 'getHouseListByTambonId':
            $paramUri = '';
            foreach ($params AS $key=>$val){
              $paramUri .= "&".$key."=".$val;
            }
            $url = $this->host."?module=".$this->_MOD."&type=ws&func=".$func.$paramUri;
            
            $xml = @file_get_contents($url);
            $pxml = simplexml_load_string($xml);
            
            $pxml = $this->_toArray($pxml);
            if(is_null($pxml['datas'])){
              return $pxml;
            }else{
              return $pxml['datas'];
            }
            break;
      case 'getHouseListByVillageId':
            $paramUri = '';
            foreach ($params AS $key=>$val){
              $paramUri .= "&".$key."=".$val;
            }
            $url = $this->host."?module=".$this->_MOD."&type=ws&func=".$func.$paramUri;
            
            $xml = @file_get_contents($url);
            $pxml = simplexml_load_string($xml);
            
            $pxml = $this->_toArray($pxml);
            if(is_null($pxml['datas'])){
              return $pxml;
            }else{
              return $pxml['datas'];
            }
            break;
      case 'getLastPersonId':
            $paramUri = '';
            foreach ($params AS $key=>$val){
              $paramUri .= "&".$key."=".$val;
            }
            $url = $this->host."?module=".$this->_MOD."&type=ws&func=".$func.$paramUri;
            
            $xml = @file_get_contents($url);
            $pxml = simplexml_load_string($xml);
            
            $pxml = $this->_toArray($pxml);
            if(is_null($pxml['datas'])){
              return $pxml;
            }else{
              return $pxml['datas'];
            }
            break;
      case 'getPersonListByHouseId':
            $paramUri = '';
            foreach ($params AS $key=>$val){
              $paramUri .= "&".$key."=".$val;
            }
            $url = $this->host."?module=".$this->_MOD."&type=ws&func=".$func.$paramUri;
            
            $xml = @file_get_contents($url);
            $pxml = simplexml_load_string($xml);
            
            $pxml = $this->_toArray($pxml);
            if(is_null($pxml['datas'])){
              return $pxml;
            }else{
              return $pxml['datas'];
            }
            break;
      case 'getPersonListByVillageId':
            $paramUri = '';
            foreach ($params AS $key=>$val){
              $paramUri .= "&".$key."=".$val;
            }
            $url = $this->host."?module=".$this->_MOD."&type=ws&func=".$func.$paramUri;
            
            $xml = @file_get_contents($url);
            $pxml = simplexml_load_string($xml);
            
            $pxml = $this->_toArray($pxml);
            if(is_null($pxml['datas'])){
              return $pxml;
            }else{
              return $pxml['datas'];
            }
            break;
      case 'getPersonQuantityByDistrictId':
            $paramUri = '';
            foreach ($params AS $key=>$val){
              $paramUri .= "&".$key."=".$val;
            }
            $url = $this->host."?module=".$this->_MOD."&type=ws&func=".$func.$paramUri;
            
            $xml = @file_get_contents($url);
            $pxml = simplexml_load_string($xml);
            
            $pxml = $this->_toArray($pxml);
            if(is_null($pxml['datas'])){
              return $pxml;
            }else{
              return $pxml['datas'];
            }
            break;
      case 'getPersonQuantityByHouseId':
            $paramUri = '';
            foreach ($params AS $key=>$val){
              $paramUri .= "&".$key."=".$val;
            }
            $url = $this->host."?module=".$this->_MOD."&type=ws&func=".$func.$paramUri;
            
            $xml = @file_get_contents($url);
            $pxml = simplexml_load_string($xml);
            
            $pxml = $this->_toArray($pxml);
            if(is_null($pxml['datas'])){
              return $pxml;
            }else{
              return $pxml['datas'];
            }
            break;
      case 'getPersonQuantityByProvinceId':
            $paramUri = '';
            foreach ($params AS $key=>$val){
              $paramUri .= "&".$key."=".$val;
            }
            $url = $this->host."?module=".$this->_MOD."&type=ws&func=".$func.$paramUri;
            
            $xml = @file_get_contents($url);
            $pxml = simplexml_load_string($xml);
            
            $pxml = $this->_toArray($pxml);
            if(is_null($pxml['datas'])){
              return $pxml;
            }else{
              return $pxml['datas'];
            }
            break;
      case 'getPersonQuantityByTambonId':
            $paramUri = '';
            foreach ($params AS $key=>$val){
              $paramUri .= "&".$key."=".$val;
            }
            $url = $this->host."?module=".$this->_MOD."&type=ws&func=".$func.$paramUri;
            
            $xml = @file_get_contents($url);
            $pxml = simplexml_load_string($xml);
            
            $pxml = $this->_toArray($pxml);
            if(is_null($pxml['datas'])){
              return $pxml;
            }else{
              return $pxml['datas'];
            }
            break;
      case 'getPersonQuantityByVillageId':
            $paramUri = '';
            foreach ($params AS $key=>$val){
              $paramUri .= "&".$key."=".$val;
            }
            $url = $this->host."?module=".$this->_MOD."&type=ws&func=".$func.$paramUri;
            
            $xml = @file_get_contents($url);
            $pxml = simplexml_load_string($xml);
            
            $pxml = $this->_toArray($pxml);
            if(is_null($pxml['datas'])){
              return $pxml;
            }else{
              return $pxml['datas'];
            }
            break;
      case 'getProvinceDetailById':
            $paramUri = '';
            foreach ($params AS $key=>$val){
              $paramUri .= "&".$key."=".$val;
            }
            $url = $this->host."?module=".$this->_MOD."&type=ws&func=".$func.$paramUri;
            
            $xml = @file_get_contents($url);
            $pxml = simplexml_load_string($xml);
            
            $pxml = $this->_toArray($pxml);
            if(is_null($pxml['datas'])){
              return $pxml;
            }else{
              return $pxml['datas'];
            }
            break;
      case 'getDistrictDetailById':
            $paramUri = '';
            foreach ($params AS $key=>$val){
              $paramUri .= "&".$key."=".$val;
            }
            $url = $this->host."?module=".$this->_MOD."&type=ws&func=".$func.$paramUri;
            
            $xml = @file_get_contents($url);
            $pxml = simplexml_load_string($xml);
            
            $pxml = $this->_toArray($pxml);
            if(is_null($pxml['datas'])){
              return $pxml;
            }else{
              return $pxml['datas'];
            }
            break;
      case 'getTambonDetailById':
            $paramUri = '';
            foreach ($params AS $key=>$val){
              $paramUri .= "&".$key."=".$val;
            }
            $url = $this->host."?module=".$this->_MOD."&type=ws&func=".$func.$paramUri;
            
            $xml = @file_get_contents($url);
            $pxml = simplexml_load_string($xml);
            
            $pxml = $this->_toArray($pxml);
            if(is_null($pxml['datas'])){
              return $pxml;
            }else{
              return $pxml['datas'];
            }
            break;
      case 'getVillageDetailById':
            $paramUri = '';
            foreach ($params AS $key=>$val){
              $paramUri .= "&".$key."=".$val;
            }
            $url = $this->host."?module=".$this->_MOD."&type=ws&func=".$func.$paramUri;
            
            $xml = @file_get_contents($url);
            $pxml = simplexml_load_string($xml);
            
            $pxml = $this->_toArray($pxml);
            if(is_null($pxml['datas'])){
              return $pxml;
            }else{
              return $pxml['datas'];
            }
            break;
      case 'getPersonListByTambonId':
            $paramUri = '';
            foreach ($params AS $key=>$val){
              $paramUri .= "&".$key."=".$val;
            }
            $url = $this->host."?module=".$this->_MOD."&type=ws&func=".$func.$paramUri;
            
            $xml = @file_get_contents($url);
            $pxml = simplexml_load_string($xml);
            
            $pxml = $this->_toArray($pxml);
            if(is_null($pxml['datas'])){
              return $pxml;
            }else{
              return $pxml['datas'];
            }
            break;
      case 'getPersonListByDistrictId':
            $paramUri = '';
            foreach ($params AS $key=>$val){
              $paramUri .= "&".$key."=".$val;
            }
            $url = $this->host."?module=".$this->_MOD."&type=ws&func=".$func.$paramUri;
            
            $xml = @file_get_contents($url);
            $pxml = simplexml_load_string($xml);
            
            $pxml = $this->_toArray($pxml);
            if(is_null($pxml['datas'])){
              return $pxml;
            }else{
              return $pxml['datas'];
            }
            break;
      case 'getPersonListByProvinceId':
            $paramUri = '';
            foreach ($params AS $key=>$val){
              $paramUri .= "&".$key."=".$val;
            }
            $url = $this->host."?module=".$this->_MOD."&type=ws&func=".$func.$paramUri;
            
            $xml = @file_get_contents($url);
            $pxml = simplexml_load_string($xml);
            
            $pxml = $this->_toArray($pxml);
            if(is_null($pxml['datas'])){
              return $pxml;
            }else{
              return $pxml['datas'];
            }
            break;
      default :
            return FALSE;
    }
  }
  
  private function _toArray($obj, $level=0) {
      
      $items = array();
      
      if(!is_object($obj)) return $items;
          
      $child = (array)$obj;

      if(sizeof($child)>1) {
          foreach($child as $aa=>$bb) {
              if(is_array($bb)) {
                  foreach($bb as $ee=>$ff) {
                      if(!is_object($ff)) {
                          $items[$aa][$ee] = $ff;
                      } else
                      if(get_class($ff)=='SimpleXMLElement') {
                          $items[$aa][$ee] = $this->_toArray($ff,$level+1);
                      }
                  }
              } else
              if(!is_object($bb)) {
                  $items[$aa] = $bb;
              } else
              if(get_class($bb)=='SimpleXMLElement') {
                  $items[$aa] = $this->_toArray($bb,$level+1);
              }
          }
      } else
      if(sizeof($child)>0) {
          foreach($child as $aa=>$bb) {
              if(!is_array($bb)&&!is_object($bb)) {
                  $items[$aa] = $bb;
              } else
              if(is_object($bb)) {
                  $items[$aa] = $this->_toArray($bb,$level+1);
              } else {
                  foreach($bb as $cc=>$dd) {
                      if(!is_object($dd)) {
                          $items[$obj->getName()][$cc] = $dd;
                      } else
                      if(get_class($dd)=='SimpleXMLElement') {
                          $items[$obj->getName()][$cc] = $this->_toArray($dd,$level+1);
                      }
                  }
              }
          }
      }
      return $items;
  }
  public function getErrorMessage(){
    return $this->ERROR;
  }
}
?>